package ch.fhnw.kosmonauten.client.gui;

import javax.swing.JOptionPane;

import ch.fhnw.kosmonauten.client.net.ServerConnection;
import ch.fhnw.kosmonauten.model.User;
import ch.fhnw.kosmonauten.net.LoginRequest;
import ch.fhnw.kosmonauten.net.Request;
import ch.fhnw.kosmonauten.net.Response;

/**
 * Diese Klasse stellt das Hauptfenster dar.
 * 
 * @author Fabian Siegrist
 * @author Oliver Freiermuth
 * @author Mikhail Onoufrienko
 * @author Risto Milani
 * @version $Revision: 203 $
 *          File name: $HeadURL: http://iching-kosmonauten.googlecode.com/svn/I-Ching/src/ch/fhnw/kosmonauten/client/gui/Login.java $
 *          Last modified:$Date: 2013-05-27 15:49:27 +0000 (Mon, 27 May 2013) $
 *          Last modified by: $Author: molotok.mihon@gmail.com $
 */

// class definition
public class Login extends javax.swing.JFrame {

	// attributes
	private ServerConnection sc;
	private User user;

	// constructors
	/**
	 * Creates new form Login
	 */
	public Login() {
		initComponents();
		this.setLocationRelativeTo(null); // zentriert das Fenster
		this.setResizable(false);
		this.setVisible(true);
	}

	// methods
	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	/**
	 * 
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jToolBar1 = new javax.swing.JToolBar();
		jPanel1 = new javax.swing.JPanel();
		jLabel1 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		jPasswordField1 = new javax.swing.JPasswordField();
		jFormattedTextField1 = new javax.swing.JFormattedTextField();
		jButton1 = new javax.swing.JButton();

		jToolBar1.setRollover(true);

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("Kosmonauten - I Ching - Login");

		jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(
				javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), "Bitte loggen Sie sich ein"));

		jLabel1.setText("Benutzername:");

		jLabel2.setText("Passwort:");

		jButton1.setText("Einloggen");
		jButton1.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
				jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout
				.setHorizontalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addGap(40, 40, 40)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																jPanel1Layout
																		.createSequentialGroup()
																		.addGap(4,
																				4,
																				4)
																		.addComponent(
																				jButton1))
														.addGroup(
																jPanel1Layout
																		.createSequentialGroup()
																		.addGroup(
																				jPanel1Layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addComponent(
																								jLabel1)
																						.addComponent(
																								jLabel2))
																		.addGap(18,
																				18,
																				18)
																		.addGroup(
																				jPanel1Layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING,
																								false)
																						.addComponent(
																								jPasswordField1,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								127,
																								Short.MAX_VALUE)
																						.addComponent(
																								jFormattedTextField1))))
										.addContainerGap(29, Short.MAX_VALUE)));
		jPanel1Layout
				.setVerticalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addGap(26, 26, 26)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel1)
														.addComponent(
																jFormattedTextField1,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGap(24, 24, 24)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel2)
														.addComponent(
																jPasswordField1,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGap(18, 18, 18)
										.addComponent(jButton1)
										.addContainerGap(27, Short.MAX_VALUE)));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup()
						.addGap(24, 24, 24)
						.addComponent(jPanel1,
								javax.swing.GroupLayout.PREFERRED_SIZE,
								javax.swing.GroupLayout.DEFAULT_SIZE,
								javax.swing.GroupLayout.PREFERRED_SIZE)
						.addContainerGap(24, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup()
						.addContainerGap()
						.addComponent(jPanel1,
								javax.swing.GroupLayout.PREFERRED_SIZE,
								javax.swing.GroupLayout.DEFAULT_SIZE,
								javax.swing.GroupLayout.PREFERRED_SIZE)
						.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)));

		pack();
	}// </editor-fold>

	/**
	 * @param evt
	 */
	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

		if (jFormattedTextField1.getText().length() > 0
				&& jPasswordField1.getPassword().length > 0) {
			sc = new ServerConnection();
			Response resp = sc.sendRequest(new Request(new LoginRequest(
					jFormattedTextField1.getText(), new String(jPasswordField1
							.getPassword())), LoginRequest.type));

			if (resp == null)
				new ErrorDialog("Ein Fehler ist aufgetretten");
			else if (resp.getContent() instanceof User) {
				user = (User) resp.getContent();

				if (user.getUsername() == null)
					new ErrorDialog(7, JOptionPane.INFORMATION_MESSAGE, new User());
				else {
					this.dispose();
					MainWindow fMain = new MainWindow(user);
				}
			} else {
				new ErrorDialog(resp.getContent().toString());
			}
		} else
			new ErrorDialog("Ihre Logindaten sind falsch");

	}

	// Variables declaration - do not modify
	private javax.swing.JButton jButton1;
	private javax.swing.JFormattedTextField jFormattedTextField1;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JPasswordField jPasswordField1;
	private javax.swing.JToolBar jToolBar1;
	// End of variables declaration

}
